import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import monacoEditorPlugin from 'vite-plugin-monaco-editor';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    plugins: [
      vue(),
      monacoEditorPlugin({}),
    ],

    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '#': resolve(__dirname, 'types'),
      },
    },

    server: {
      port: Number(env.VITE_PORT) || 5173,
      host: true,
      open: true,
      proxy: env.VITE_PROXY === 'true' ? {
        '/router': {
          target: env.VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path,
        },
      } : undefined,
    },

    build: {
      target: 'es2015',
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: false,
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ['vue', 'vue-router', 'pinia'],
            antd: ['ant-design-vue', '@ant-design/icons-vue'],
            monaco: ['monaco-editor'],
          },
        },
      },
    },
  };
});
